package com.hyphenate.easeui.widget.chatrow;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.text.Spannable;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TextView.BufferType;

import com.google.gson.Gson;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMMessage.ChatType;
import com.hyphenate.chat.EMTextMessageBody;
import com.hyphenate.easeui.utils.EaseSmileUtils;
import com.hyphenate.exceptions.HyphenateException;
import com.hyphenate.util.DensityUtil;
import com.joplus.housekeeper.R;
import com.joplus.housekeeper.bean.BagMoneyBean;
import com.joplus.housekeeper.bean.ChatCard;
import com.joplus.housekeeper.network.RequestHelper;
import com.joplus.housekeeper.network.TextCallback;

import com.joplus.housekeeper.utils.L;
import com.joplus.housekeeper.utils.StringUtils;
import com.joplus.housekeeper.utils.T;

import org.json.JSONObject;

import de.hdodenhof.circleimageview.CircleImageView;

/// type 4  special 1 是医生特殊需求
public class EaseChatRowText_BAG extends EaseChatRow {

    private TextView contentView;

    private TextView tv_result;

    private TextView tv_title;
    private TextView tv_1;
    private TextView tv_2;
    private TextView tv;
    private CircleImageView iv;
    private LinearLayout ll_button;


    public EaseChatRowText_BAG(Context context, EMMessage message, int position, BaseAdapter adapter) {
        super(context, message, position, adapter);

    }

    @Override
    protected void onInflatView() {
        inflater.inflate(message.direct() == EMMessage.Direct.RECEIVE ?
                R.layout.ease_row_received_message_bag : R.layout.ease_row_sent_message, this);


    }

    @Override
    protected void onFindViewById() {
        contentView = (TextView) findViewById(R.id.tv_chatcontent);
        //card

        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_1 = (TextView) findViewById(R.id.tv_1);
        tv_2 = (TextView) findViewById(R.id.tv_2);
        tv = (TextView) findViewById(R.id.tv);
        iv = (CircleImageView) findViewById(R.id.iv);
        ll_button = (LinearLayout) findViewById(R.id.ll_button);


    }


    @Override
    public void onSetUpView() {
        EMTextMessageBody txtBody = (EMTextMessageBody) message.getBody();


        Spannable span = EaseSmileUtils.getSmiledText(context, txtBody.getMessage());
        // 设置内容
        contentView.setText(span, BufferType.SPANNABLE);


        String msg = txtBody.getMessage();
        try {
            new JSONObject(msg);
            Gson gson = new Gson();
            ChatCard chatCard = gson.fromJson(txtBody.getMessage(), ChatCard.class);
            bubbleLayout.setVisibility(View.GONE);
            contentView.setVisibility(View.GONE);
            String subject = chatCard.getSubject();

            setTip(chatCard);

        } catch (Exception e) {
            L.e("onSetUpView()", "position-->" + position + "--error-->" + e.toString());
            bubbleLayout.setVisibility(View.VISIBLE);
//            layoutTip.setVisibility(View.GONE);
            contentView.setVisibility(View.VISIBLE);


        }
//        handleTextMessage();

    }


    private void setTip(final ChatCard chatCard) {
        //tv_title.setText();
        StringUtils.setText(tv_title, chatCard.getStoredMsg());
        if ("Find".equals(chatCard.getResult())) {
            tv_1.setText("查看");
            tv_1.setPadding(DensityUtil.dip2px(context, 50), DensityUtil.dip2px(context, 4f), DensityUtil.dip2px(context, 50), DensityUtil.dip2px(context, 4f));
            tv.setText("恭喜您");

            if ("1".equals(chatCard.getType())) {
                iv.setImageResource(R.drawable.bag_f);
            } else if ("2".equals(chatCard.getType())) {
                iv.setImageResource(R.drawable.wine_f);
            } else if ("3".equals(chatCard.getType())) {
                iv.setImageResource(R.drawable.private_flight_f);
            } else if ("4".equals(chatCard.getType()) || "5".equals(chatCard.getType())) {
                if ("1".equals(chatCard.getIsSpecialRequirement())) {
                    iv.setImageResource(R.drawable.doctor_f);
                } else {
                    iv.setImageResource(R.drawable.doctors_f);
                }
            } else if ("3".equals(chatCard.getType())) {
                iv.setImageResource(R.drawable.air_plan_f);
            }

            tv_1.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    //订单详情

                }
            });

        } else {
            tv.setText("抱歉");
            if ("1".equals(chatCard.getType())) {
                iv.setImageResource(R.drawable.bag_u);
            } else if ("2".equals(chatCard.getType())) {
                iv.setImageResource(R.drawable.wine_u);
            } else if ("3".equals(chatCard.getType())) {
                iv.setImageResource(R.drawable.private_flight_u);
                ll_button.setVisibility(GONE);
            } else if ("4".equals(chatCard.getType()) || "5".equals(chatCard.getType())) {
                if ("1".equals(chatCard.getIsSpecialRequirement())) {
                    iv.setImageResource(R.drawable.doctor_u);
                } else {
                    iv.setImageResource(R.drawable.doctors_u);
                }
            } else if ("5".equals(chatCard.getType())) {
                iv.setImageResource(R.drawable.ari_plan_u);
                ll_button.setVisibility(GONE);
            }

            tv_1.setText("再找别的");
            if ("4".equals(chatCard.getType()) || "5".equals(chatCard.getType()) || "6".equals(chatCard.getType())) {
                tv_2.setVisibility(GONE);
            } else {
                tv_2.setText("库存中看看");
                tv_2.setVisibility(VISIBLE);
            }


            tv_1.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    //定制
                    if (StringUtils.isEmpty(chatCard.getStoredId())) {
                        T.showShort(context, "服务器数据错误");
                        return;
                    }



                }
            });
            tv_2.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    //包包商店
                    if (StringUtils.isEmpty(chatCard.getStoredId())) {
                        T.showShort(context, "服务器数据错误");
                        return;
                    }



                }
            });
        }


//        tv_result.setOnClickListener(new OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent=new Intent(context, EvaluateActivity.class);
//                context.startActivity(intent);
//            }
//        });

    }


    protected void handleTextMessage() {
        if (message.direct() == EMMessage.Direct.SEND) {
            setMessageSendCallback();
            switch (message.status()) {
                case CREATE:
                    progressBar.setVisibility(View.GONE);
                    statusView.setVisibility(View.VISIBLE);
                    // 发送消息
                    //               sendMsgInBackground(message);
                    break;
                case SUCCESS: // 发送成功
                    progressBar.setVisibility(View.GONE);
                    statusView.setVisibility(View.GONE);
                    break;
                case FAIL: // 发送失败
                    progressBar.setVisibility(View.GONE);
                    statusView.setVisibility(View.VISIBLE);
                    break;
                case INPROGRESS: // 发送中
                    progressBar.setVisibility(View.VISIBLE);
                    statusView.setVisibility(View.GONE);
                    break;
                default:
                    break;
            }
        } else {
            if (!message.isAcked() && message.getChatType() == ChatType.Chat) {
                try {
                    EMClient.getInstance().chatManager().ackMessageRead(message.getFrom(), message.getMsgId());
                } catch (HyphenateException e) {
                    e.printStackTrace();
                }
            }

        }
    }

    @Override
    protected void onUpdateView() {
        adapter.notifyDataSetChanged();

    }

    @Override
    protected void onBubbleClick() {
        L.e("点击了聊天文字");
    }


}
